// 封装请求
import axios from 'axios'
import { ElMessage } from 'element-plus'
import Cookies from 'js-Cookie'
import { NewAccess } from './api'
axios.defaults.baseURL = "http://1.14.241.161:9099/api"
// 请求
const http = axios.create({
    timeout: 6000
})
// 请求拦截
http.interceptors.request.use((config: any) => {
    // 请求头设置
    const AccessToken = Cookies.get('AccessToken') || ''
    const RefreshToken = Cookies.get('RefreshToken') || ''
    config.headers.common['AccessToken'] = AccessToken
    config.headers.common['RefreshToken'] = RefreshToken
    return config
}, err => {

})

interface Suc {
    data: code,
}
interface T {
    message: string,
    response: { data: code }
}

interface code {
    code: keyof typeof codes,
    msg: string,
    access: string,
    refresh: string,

}
let codes = {
    200: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
            duration: 500
        })
    },
    201: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
        })
    },
    204: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
        })
    },
    300: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    403: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    600005: (arr: any) => {
        // ElMessage({
        //     message: arr.data.msg,
        //     type: 'warning',
        // })
    },
    600002: (arr: any) => {
        // ElMessage({
        //     message: arr.data.msg,
        //     type: 'warning',
        // })
    },
    600014: (arr: any) => {
        ElMessage({
            message: "原密码错误！",
            type: 'error',
        })
    },
    400: (arr: any) => {

        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    401: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    611111: (arr: any) => {

    },
    600003: (arr: any) => {

    },
    600004: (arr: any) => {

    },
    600000: (arr: any) => {
        ElMessage({
            message: "token已刷新，请重复刚才操作或者刷新试试",
            type: 'warning',
        })
        NewAccess().then((res: any) => {

            Cookies.set("AccessToken", res.access)
        })
    },
    600009: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'error',
        })
    },
    600010: (arr: any) => {

    },
    600008: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'error',
        })
    },
    600011: (arr: any) => {
        // ElMessage({
        //     message: "该城市测评已开放，请选择开放地区",
        //     type: 'success',
        // })
    },
    600012: (arr: any) => {

    },
    600015: (arr: any) => {

    },
    600017: (arr: any) => {

    },
    404: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    422: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    500: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'error',
        })
    },

}
let errcodes = {
    0: (arr: any) => {

    },
    1: (arr: any) => {

    },
    2: (arr: any) => {

    },
    3: (arr: any) => {

    },
    200: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
            duration: 500
        })
    },
    201: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
        })
    },
    204: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'success',
        })
    },
    300: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    403: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    600005: (arr: any) => {
        // ElMessage({
        //     message: arr.data.msg,
        //     type: 'warning',
        // })
    },
    600002: (arr: any) => {
        // ElMessage({
        //     message: arr.data.msg,
        //     type: 'warning',
        // })
    },
    600014: (arr: any) => {
        ElMessage({
            message: "原密码错误！",
            type: 'error',
        })
    },
    600015: (arr: any) => {

    },
    400: (arr: any) => {

        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    401: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    611111: (arr: any) => {

    },
    600003: (arr: any) => {

    },
    600004: (arr: any) => {

    },
    600000: (arr: any) => {
        ElMessage({
            message: "token已刷新，请重复刚才操作或者刷新试试",
            type: 'warning',
        })
        NewAccess().then((res: any) => {

            Cookies.set("AccessToken", res.access)
        })
    },
    600009: (arr: any) => {

    },
    600010: (arr: any) => {

    },
    600001: (arr: any) => {

    },
    600011: (arr: any) => {
        // ElMessage({
        //     message: "该城市测评已开放，请选择开放地区",
        //     type: 'success',
        // })
    },
    404: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    422: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'warning',
        })
    },
    500: (arr: any) => {
        ElMessage({
            message: arr.data.msg,
            type: 'error',
        })
    },

}
// 响应拦截
http.interceptors.response.use((arr: Suc) => {
    // 对响应码的处理
    if (arr.data.code)
        codes[arr.data.code](arr)
    return arr.data
}, (err: T) => {
    if (err.response === undefined) {
        ElMessage({
            message: err.message,
            type: 'error',
        })
        if (err.message === 'timeout of 5000ms exceeded') {
            return {
                code: 0,
                msg: "网络超时"
            }
        }
        return {
            code: 0,
            msg: "网络错误"
        }
    }

    if (err.response !== undefined) {
        codes[err.response.data.code](err.response)
        return err.response.data
    }

})
// 返出
export default http
